import { adminApi } from '@/api/index'

const role = {
    namespaced: true,
    state: {
        admins: {
            list: [],
            pageNum: 1,
            total: 0
        },
        admin: {
            roleIds: [],
            id: 0,
            name: '',
            username:'',
            password:'',
            avatar:'',
            status: 1,
            sort: 0
        }
    },
    getters: {
        adminList(state) {
            return state.admins.list
        },
        roleAll(state, getters, rootState, rootGetters) {
            return rootState.role.roles.list
        }
    },
    mutations: {
        SET_ADMINS(state, { list, pageNum, total }) {
            state.admins.list = list
            state.admins.pageNum = pageNum
            state.admins.total = total
        },
        SET_ADMIN(state, { roleIds,id, name, username,password,avatar,status, sort }) {
            state.admin.roleIds=roleIds
            state.admin.id = id
            state.admin.name = name
            state.admin.username = username
            state.admin.password =password
            state.admin.avatar = avatar
            state.admin.status = status
            state.admin.sort = sort
        },
        SET_AVATAR(state,avatar){
            state.admin.avatar = avatar
        }
    },
    actions: {
        GET_ADMINS({ commit }, param) {
            return new Promise((resolve, reject) => {
                adminApi.selectList(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        commit('SET_ADMINS', data)
                        resolve(message)
                    } else {
                        commit('SET_ADMINS', [])
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })

        },

        GET_ADMIN_BY_ID({ commit }, id) {
            return new Promise((resolve, reject) => {
                let admin = {
                    roleIds: [],
                    id: 0,
                    name: '',
                    username:'',
                    password:'',
                    avatar:'',
                    status: 1,
                    sort: 0
                }
                adminApi.selectById(id).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        admin.id = data.id
                        admin.name = data.name
                        admin.username = data.username
                        admin.password = data.password
                        admin.avatar = data.avatar
                        admin.status = data.status
                        admin.sort = data.sort
                        adminApi.selectRolesByAdminId(id).then(response => {
                            const { code, message, data } = response.data
                            if (code === 200) {
                                admin.roleIds = data.map(role => role.id)
                                commit('SET_ADMIN', admin)
                                resolve(message)
                            } else {
                                reject(message)
                            }
                        }).catch(error => {
                            reject(error)
                        })
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },

        ADD_ADMIN({ commit }, param) {
            return new Promise((resolve, reject) => {
                adminApi.add(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        UPDATE_ADMIN({commit},param){
            return new Promise((resolve, reject) => {
                adminApi.update(param).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        DELETE_ADMIN_BY_IDS({commit},ids){
            return new Promise((resolve, reject) => {
                adminApi.deleteBatch(ids).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        UPLOAD_AVATAR({commit},formData){
            return new Promise((resolve, reject) => {
                adminApi.uploadAvatar(formData).then(response => {
                    const { code, message, data } = response.data
                    if (code === 200) {
                        commit('SET_AVATAR',data)
                        resolve(message)
                    } else {
                        reject(message)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        }
    }
}

export default role